<template>
  <div class="hotel-list-page">
    <!-- 顶部导航 -->
    <header class="page-header">
      <div class="container">
        <div class="nav-brand">
          <a href="/" class="brand-link">酒店预订系统</a>
        </div>
        <nav class="nav-menu">
          <a href="/" class="nav-link">首页</a>
          <a href="/fouter/hotels" class="nav-link active">酒店列表</a>
          <a href="/hotel/public/orders" class="nav-link">我的订单</a>
          <a href="/login" class="nav-link">登录</a>
        </nav>
      </div>
    </header>

    <!-- 主要内容 -->
    <main class="main-content">
      <div class="container">
        <div class="page-title">
          <h1>酒店列表</h1>
          <p>无需登录，直接预订心仪酒店</p>
        </div>

        <!-- 搜索和筛选 -->
        <div class="search-section">
          <a-input-search
            v-model:value="searchKeyword"
            placeholder="搜索酒店名称或地址"
            enter-button="搜索"
            size="large"
            @search="handleSearch"
            style="max-width: 500px; margin-bottom: 16px;"
          />
          <div class="filters">
            <a-select v-model:value="starFilter" placeholder="星级筛选" style="width: 120px" @change="handleFilter">
              <a-select-option value="">全部星级</a-select-option>
              <a-select-option value="5">五星级</a-select-option>
              <a-select-option value="4">四星级</a-select-option>
              <a-select-option value="3">三星级</a-select-option>
            </a-select>
          </div>
        </div>

        <!-- 酒店网格 -->
        <div class="hotels-grid" v-if="!loading">
          <div class="hotel-card" v-for="hotel in filteredHotels" :key="hotel.id" @click="goToDetail(hotel.id)">
            <div class="hotel-image">
              <img :src="hotel.imageUrl || fallbackImg(hotel.id)" :alt="hotel.name" />
              <div class="hotel-star">⭐ {{ hotel.star || 3 }}</div>
            </div>
            <div class="hotel-info">
              <h3 class="hotel-name">{{ hotel.name }}</h3>
              <p class="hotel-address">{{ hotel.address }}</p>
              <p class="hotel-desc">{{ hotel.description || '欢迎入住，舒适之选' }}</p>
              <div class="hotel-meta">
                <span class="hotel-price">￥{{ hotel.priceAvg }}/晚起</span>
                <span class="hotel-tel" v-if="hotel.tel">📞 {{ hotel.tel }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 加载状态 -->
        <div v-if="loading" class="loading-container">
          <a-spin size="large" />
          <p>正在加载酒店信息...</p>
        </div>

        <!-- 空状态 -->
        <div v-if="!loading && filteredHotels.length === 0" class="empty-container">
          <a-empty description="暂无酒店信息" />
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { hotelAPI } from '@/api/hotel'

const router = useRouter()

// 响应式数据
const hotels = ref([])
const loading = ref(false)
const searchKeyword = ref('')
const starFilter = ref('')

// 计算属性 - 过滤后的酒店列表
const filteredHotels = computed(() => {
  let result = hotels.value

  // 按关键词搜索
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase()
    result = result.filter(hotel => 
      hotel.name.toLowerCase().includes(keyword) ||
      hotel.address.toLowerCase().includes(keyword) ||
      (hotel.description && hotel.description.toLowerCase().includes(keyword))
    )
  }

  // 按星级筛选
  if (starFilter.value) {
    result = result.filter(hotel => hotel.star == starFilter.value)
  }

  return result
})

// 加载酒店数据
const loadHotels = async () => {
  loading.value = true
  try {
    const res = await hotelAPI.list(1, 50)
    const payload = res.data?.data || res.data || {}
    const list = Array.isArray(payload) ? payload : (payload.list || [])
    hotels.value = list.map(x => ({
      id: x.id,
      name: x.name,
      address: x.address,
      tel: x.tel,
      star: x.star,
      priceAvg: x.priceAvg || x.price_avg,
      imageUrl: x.imageUrl || x.image_url,
      description: x.description
    }))
  } catch (error) {
    console.error('加载酒店数据失败:', error)
  } finally {
    loading.value = false
  }
}

// 搜索处理
const handleSearch = (value) => {
  searchKeyword.value = value
}

// 筛选处理
const handleFilter = () => {
  // 筛选逻辑已在计算属性中处理
}

// 跳转到酒店详情
const goToDetail = (hotelId) => {
  router.push(`/fouter/hotels/${hotelId}`)
}

// 默认图片
const fallbackImg = (id) => `https://picsum.photos/seed/hotel-${id}/600/360`

// 组件挂载时加载数据
onMounted(() => {
  loadHotels()
})
</script>

<style scoped>
.hotel-list-page {
  min-height: 100vh;
  background: #f5f5f5;
}

/* 页面头部 */
.page-header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.page-header .container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 64px;
}

.nav-brand .brand-link {
  text-decoration: none;
  color: #1890ff;
  font-size: 24px;
  font-weight: 600;
}

.nav-menu {
  display: flex;
  gap: 24px;
}

.nav-link {
  text-decoration: none;
  color: #666;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 4px;
  transition: all 0.3s;
}

.nav-link:hover,
.nav-link.active {
  color: #1890ff;
  background: #f0f8ff;
}

/* 主要内容 */
.main-content {
  padding: 40px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.page-title {
  text-align: center;
  margin-bottom: 32px;
}

.page-title h1 {
  font-size: 32px;
  color: #333;
  margin: 0 0 8px 0;
}

.page-title p {
  color: #666;
  font-size: 16px;
  margin: 0;
}

/* 搜索区域 */
.search-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 32px;
}

.filters {
  display: flex;
  gap: 12px;
  justify-content: center;
}

/* 酒店网格 */
.hotels-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
}

.hotel-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  cursor: pointer;
}

.hotel-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.hotel-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.hotel-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.hotel-star {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}

.hotel-info {
  padding: 20px;
}

.hotel-name {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.hotel-address {
  color: #666;
  font-size: 14px;
  margin: 0 0 8px 0;
}

.hotel-desc {
  color: #999;
  font-size: 13px;
  line-height: 1.4;
  margin: 0 0 12px 0;
  height: 36px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.hotel-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.hotel-price {
  color: #e53935;
  font-size: 16px;
  font-weight: 600;
}

.hotel-tel {
  color: #666;
  font-size: 12px;
}

/* 加载和空状态 */
.loading-container,
.empty-container {
  text-align: center;
  padding: 60px 0;
}

.loading-container p {
  margin-top: 16px;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header .container {
    flex-direction: column;
    height: auto;
    padding: 16px 20px;
  }

  .nav-menu {
    margin-top: 16px;
    flex-wrap: wrap;
    justify-content: center;
  }

  .page-title h1 {
    font-size: 24px;
  }

  .hotels-grid {
    grid-template-columns: 1fr;
  }

  .search-section {
    align-items: stretch;
  }

  .filters {
    justify-content: stretch;
  }
}
</style>



